package com.castle.fortress.admin.study.course.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.study.course.entity.StudyCourseChapterCoursewareEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseChapterCoursewareDto;
import com.castle.fortress.admin.study.course.mapper.StudyCourseChapterCoursewareMapper;
import com.castle.fortress.admin.study.course.service.StudyCourseChapterCoursewareService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 章节课件表 服务实现类
 *
 * @author castle
 * @since 2023-01-31
 */
@Service
public class StudyCourseChapterCoursewareServiceImpl extends ServiceImpl<StudyCourseChapterCoursewareMapper, StudyCourseChapterCoursewareEntity> implements StudyCourseChapterCoursewareService {
    /**
    * 获取查询条件
    * @param studyCourseChapterCoursewareDto
    * @return
    */
    public QueryWrapper<StudyCourseChapterCoursewareEntity> getWrapper(StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto){
        QueryWrapper<StudyCourseChapterCoursewareEntity> wrapper= new QueryWrapper();
        if(studyCourseChapterCoursewareDto != null){
            StudyCourseChapterCoursewareEntity studyCourseChapterCoursewareEntity = ConvertUtil.transformObj(studyCourseChapterCoursewareDto,StudyCourseChapterCoursewareEntity.class);
            wrapper.eq(studyCourseChapterCoursewareEntity.getId() != null,"id",studyCourseChapterCoursewareEntity.getId());
            wrapper.eq(studyCourseChapterCoursewareEntity.getChapterId() != null,"chapter_id",studyCourseChapterCoursewareEntity.getChapterId());
            wrapper.eq(studyCourseChapterCoursewareEntity.getCourseId() != null,"course_id",studyCourseChapterCoursewareEntity.getCourseId());
            wrapper.eq(studyCourseChapterCoursewareEntity.getCoursewareId() != null,"courseware_id",studyCourseChapterCoursewareEntity.getCoursewareId());
            wrapper.eq(studyCourseChapterCoursewareEntity.getSort() != null,"sort",studyCourseChapterCoursewareEntity.getSort());
            wrapper.eq(studyCourseChapterCoursewareEntity.getStatus() != null,"status",studyCourseChapterCoursewareEntity.getStatus());
            wrapper.eq(studyCourseChapterCoursewareEntity.getCreateUser() != null,"create_user",studyCourseChapterCoursewareEntity.getCreateUser());
            wrapper.eq(studyCourseChapterCoursewareEntity.getCreateTime() != null,"create_time",studyCourseChapterCoursewareEntity.getCreateTime());
            wrapper.eq(studyCourseChapterCoursewareEntity.getUpdateUser() != null,"update_user",studyCourseChapterCoursewareEntity.getUpdateUser());
            wrapper.eq(studyCourseChapterCoursewareEntity.getUpdateTime() != null,"update_time",studyCourseChapterCoursewareEntity.getUpdateTime());
            wrapper.eq(studyCourseChapterCoursewareEntity.getIsDeleted() != null,"is_deleted",studyCourseChapterCoursewareEntity.getIsDeleted());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<StudyCourseChapterCoursewareDto> pageStudyCourseChapterCourseware(Page<StudyCourseChapterCoursewareDto> page, StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto) {
        QueryWrapper<StudyCourseChapterCoursewareEntity> wrapper = getWrapper(studyCourseChapterCoursewareDto);
        Page<StudyCourseChapterCoursewareEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyCourseChapterCoursewareEntity> studyCourseChapterCoursewarePage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyCourseChapterCoursewareDto> pageDto = new Page(studyCourseChapterCoursewarePage.getCurrent(), studyCourseChapterCoursewarePage.getSize(),studyCourseChapterCoursewarePage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyCourseChapterCoursewarePage.getRecords(),StudyCourseChapterCoursewareDto.class));
        return pageDto;
    }

    @Override
    public IPage<StudyCourseChapterCoursewareDto> pageStudyCourseChapterCoursewareExtends(Page<StudyCourseChapterCoursewareDto> page, StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        StudyCourseChapterCoursewareEntity studyCourseChapterCoursewareEntity = ConvertUtil.transformObj(studyCourseChapterCoursewareDto,StudyCourseChapterCoursewareEntity.class);
        List<StudyCourseChapterCoursewareEntity> studyCourseChapterCoursewareList=baseMapper.extendsList(pageMap,studyCourseChapterCoursewareEntity);
        Long total = baseMapper.extendsCount(studyCourseChapterCoursewareEntity);
        Page<StudyCourseChapterCoursewareDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(studyCourseChapterCoursewareList,StudyCourseChapterCoursewareDto.class));
        return pageDto;
    }
    @Override
    public StudyCourseChapterCoursewareDto getByIdExtends(Long id){
        StudyCourseChapterCoursewareEntity  studyCourseChapterCoursewareEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(studyCourseChapterCoursewareEntity,StudyCourseChapterCoursewareDto.class);
    }

    @Override
    public List<StudyCourseChapterCoursewareDto> listStudyCourseChapterCourseware(StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto){
        QueryWrapper<StudyCourseChapterCoursewareEntity> wrapper = getWrapper(studyCourseChapterCoursewareDto);
        List<StudyCourseChapterCoursewareEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,StudyCourseChapterCoursewareDto.class);
    }

    @Override
    public void delByCourseId(Long courseId) {
        if(courseId!=null){
            baseMapper.delByCourseId(courseId);
        }
    }

    @Override
    public void delByCourseIds(List<Long> courseIds) {
        if(courseIds!=null && courseIds.size()>0){
            baseMapper.delByCourseIds(courseIds);
        }
    }

    @Override
    public void delByCoursewareId(Long id) {
        if(id!=null){
            baseMapper.delByCoursewareId(id);
        }
    }

    @Override
    public void delByCoursewareIds(List<Long> ids) {
        if(ids!=null && ids.size()>0){
            baseMapper.delByCoursewareIds(ids);
        }
    }

    @Override
    public List<StudyCourseChapterCoursewareEntity> findByCoursewareId(Long coursewareId) {
        return baseMapper.findByCoursewareId(coursewareId);
    }

    @Override
    public List<StudyCourseChapterCoursewareEntity> findByCoursewareIds(List<Long> ids) {
        return baseMapper.findByCoursewareIds(ids);
    }
}

